<template>
  <div class="login-container">
    <div class="blob"></div>
    <!--登录表单-->
    <div class="wrapper">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">

        <div class="title-container">
          <h2 class="title">Login</h2>
        </div>

        <el-form-item prop="username" class="input-box">
          <span class="icon"><ion-icon name="person-circle-outline"></ion-icon></span>
          <input
            type="text"
            ref="username"
            v-model="loginForm.username"
            name="username"
            tabindex="1"
            required
          >
          <label>Username</label>
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password" class="input-box">
            <span class="icon" @click="showPwd()"><ion-icon :name=lockName></ion-icon></span>
            <input
              :type=passwordType
              ref="password"
              v-model="loginForm.password"
              name="password"
              tabindex="2"
              required
              @blur="capsTooltip = false"
              @keyup.native="checkCapslock"
              @keyup.enter.native="handleLogin">
            <label>Password</label>
          </el-form-item>
        </el-tooltip>

        <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin" class="login-button">Login</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data() {
    return {
      lockName: 'lock-closed',
      //登录表单对象
      loginForm: {
        username: '',//用户名
        password: ''//密码
      },
      //表单验证规则
      loginRules: {
        username: [{ required: true, trigger: 'blur', message:"请输入用户名" }],
        password: [{ required: true, trigger: 'blur', message:"请输入密码" }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    // //如果用户名为空，则焦点聚焦到用户名输入框
    // if (this.loginForm.username === '') {
    //   this.$refs.username.focus()
    // } else if (this.loginForm.password === '') {
    //   this.$refs.password.focus()
    // }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    /**
     * 是否显示密码
     */
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
        this.lockName = 'lock-open'
      } else {
        this.passwordType = 'password'
        this.lockName = 'lock-closed'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    /**
     * 处理登陆请求
     */
    handleLogin() {
      //进行表单验证
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          //如果表单验证通过
          this.loading = true//显示加载动画
          //发送登录请求（调用Vuex中的user/login方法）
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$message({
                message: '登录成功',
                type: 'success'
              })
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
input:-webkit-autofill {
  box-shadow:0 0 0 1000px transparent inset !important; // 颜色可以随意，这里设置的是透明色
}
input:-internal-autofill-previewed,
input:-internal-autofill-selected {
  -webkit-text-fill-color: #fff !important; // 可加可不加
  transition: background-color 5000s ease-in-out 0s !important;
}

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800;900&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  min-width: 100vw;
  background: #1f1f47;
  overflow: hidden;
}

body {
  overflow: hidden;
}
.wrapper {
  position: relative;
  width: 400px;
  height: 450px;
  background: rgba(255, 255, 255, 0.15);
  box-shadow: 0 8px 32px rgba(31,38,135,.37);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-form {
  width: 320px;
}
.wrapper h2 {
  font-size: 36px;
  color: #ffffff;
  text-align: center;
}
.input-box {
  position: relative;
  width: 320px;
  margin: 30px 0;
  border-bottom: 2px solid #ffffff;
}
.input-box label {
  position: absolute;
  top: 50%;
  left: 5px;
  transform: translateY(-50%);
  font-size: 18px;
  pointer-events: none;
  color: #ffffff;
  transition: .5s;
}
.input-box input:focus ~ label,
.input-box input:valid ~ label {
  top: -5px;
}
.input-box input {
  width: 100%;
  height: 50px;
  background: transparent !important;
  border: none;
  outline: none;
  font-size: 18px;
  color: #ffffff;
  padding: 0 35px 0 5px;
}

.input-box .icon {
  position: absolute;
  right: 8px;
  color: #ffffff;
  font-size: 20px;
  line-height: 57px;
}
.login-button {
  width: 100%;
  height: 45px;
  background: #fa709a;
  border: none;
  outline: none;
  border-radius: 40px;
  cursor: pointer;
  font-size: 1em;
  color: #ffffff;
  font-weight: 800;
}
.blob {
  position: absolute;
  width: 500px;
  height: 500px;
  background: linear-gradient(180deg, rgba(47,184,255, .42) 31.77%, #5c9df1 100%);
  border-radius: 24% 76% 35% 65% / 27% 36% 64% 73%;
  transform: translate(-100px, -50px) rotate(-90deg);
  transition: 1s cubic-bezier(.07, .8, .16, 1);
  animation: animateBlob 20s infinite alternate;
}

.blob:hover {
  width: 520px;
  height: 520px;
  filter: blur(30px);
  box-shadow:
    inset 0 0 0 5px rgba(255,255,255,.6),
    inset 100px 100px 0 0 #fa709a,
    inset 200px 200px 0 0 #784ba8,
    inset 300px 300px 0 0 #2b86c5;
}

@keyframes animateBlob {
  100% {
    transform: translate(300px, 50px) rotate(-10deg);
    border-radius: 76% 24% 33% 67% / 68% 55% 45% 32%;
  }
}
</style>
